package cs4game;

public class Enemy extends MobEntity{

	
	public Enemy(){
		
	}
	


	public Enemy(Vector3D point3d, World world) {
		super(point3d, world);
	}



	protected void defineTexture() {
		texture = BlockDefs.get4DFromTerrain(8, 0, 0.6 );
	}
	
	@SuppressWarnings("unused")
	public void move(float time){
		Player p = world.getPlayer();
		
		this.velocity[0] = 0;
		//this.velocity[1] = (float) -.1;
		this.velocity[2] = 0;
		
		for(Vector3D vec: world.allBlocks.keySet()){
			if(vec.y<-1){
				System.out.println("enemy:" + this.pos.x);
				System.out.println("block:" + vec.x);
				System.out.println("player:" + p.pos.x);
				if(!((this.pos.x < vec.x && vec.x < p.pos.x) && (this.pos.z < vec.z && vec.z < p.pos.z))){
					double moveAngle = Math.atan2(p.getPos().z-this.getPos().z, p.getPos().x-this.getPos().x);
					this.velocity[0] = (float) (0.04*Math.cos(moveAngle));//.08
					this.velocity[2] = (float) (0.04*Math.sin(moveAngle));
					
				}
			}//try{if(true){do{for(double i = 0; i<0; i++){while(true){int x;}}}while(true);}}catch(Exception ex){boolean b;}
		}
		if(world.allBlocks.keySet().size()<=0){
			double moveAngle = Math.atan2(p.getPos().z-this.getPos().z, p.getPos().x-this.getPos().x);
			this.velocity[0] = (float) (0.08*Math.cos(moveAngle));
			this.velocity[2] = (float) (0.08*Math.sin(moveAngle));
		}
		
		
		
		
		//boolean b = Mouse.isButtonDown(0);
		if(!p.dead){
			float angle = (float) Math.atan2(p.getPos().z-this.getPos().z, p.getPos().x-this.getPos().x);
			//this.shootBullet(angle);
		}
		//prevClickedL = b;
		
		super.move(time);
		
		
		//System.out.println(box);
		for(MobEntity e: world.allEntities){
			box = new BoundingBox(-1*width/2, width/2, -1*height/2, height/2, -1*depth/2, depth/2);
			e.box = new BoundingBox(-1*width/2, width/2, -1*height/2, height/2, -1*depth/2, depth/2);
			//box = shiftedBoundingBox();
			if(this.shiftedBoundingBox().intersects(e.shiftedBoundingBox())){
				if(e instanceof Player){
					world.getPlayer().kill();
					world.removeEntity(this);
				}/*else if(e instanceof Bullet && (((Bullet)e).isPBullet) ){
					world.removeEntity(e);
					world.removeEntity(this);
				}*/
			}
		}
	}

	public void shootBullet(float angle) {
		float[] v = new float[3];
		v[0] = (float) (10*Math.cos(angle));
		v[1] = 0;
		v[2] = (float) (10*Math.sin(angle));
		Vector3D copyPos = pos.copy();
		copyPos.x-=width/2 - .3f;
		copyPos.y-=height/2;
		copyPos.z-=depth/2 - .3f;
		Bullet b = new Bullet(world, copyPos, v);
		b.isPBullet = false;
		world.addEntity(b);
	}
	
	public boolean collidesWithEntity(Entity other) {
		return this.shiftedBoundingBox().intersects(other.shiftedBoundingBox());
	}
}